package com.gitee.hljdrl.lite;

import android.app.Application;
import android.content.Context;

import com.gitee.hljdrl.lite.app.AppInterface;
import com.gitee.hljdrl.lite.cache.Cache;
import com.gitee.hljdrl.lite.cache.TableCache;
import com.gitee.hljdrl.lite.config.AppConf;
import com.gitee.hljdrl.lite.task.AppTask;
import com.gitee.hljdrl.lite.task.AndroidTask;
import com.gitee.hljdrl.database.DataBaseManager;

public final class Lite {


    public static Application application;
    public static AppInterface appInterface;
    private static Context mContext;
    public static Cache cache;
    public static AppTask task;
    public static AppConf appConf;

    public static void install(Builder builder) {
        mContext = builder.mContext;
        application = builder.application;
        cache = builder.kvCache;
        task = builder.task;
        appConf = builder.appConf;
        appInterface = builder.appInterface;
        DataBaseManager.install(mContext, builder.dataBaseName, builder.dataBaseVersion,builder.dataBasePassword);
        if(cache ==null) {
            cache = new TableCache();
        }
        if(task==null){
            task = new AndroidTask();
        }
    }

    public final static void unInstall(){
        if(task!=null){
            task.destroy();
            task = null;
        }
        DataBaseManager.unInstance();
        cache = null;
        mContext = null;
    }

    public static Context getContext() {
        return mContext;
    }

    public static class Builder {

         Application application;
         Context mContext;
         String dataBaseName = "database.db";

         int dataBaseVersion = 2;

         String dataBasePassword = null;

        AppConf appConf;

        AppInterface appInterface;

        Cache kvCache;

        AppTask task;

        public Builder setTask(AppTask task) {
            this.task = task;
            return this;
        }

        public Builder setKVCache(Cache kvCache) {
            this.kvCache = kvCache;
            return this;
        }

        public Builder setApplication(Application application) {
            this.application = application;
            mContext = application.getApplicationContext();
            return this;
        }

        public Builder setAppConf(AppConf appConf){
            this.appConf = appConf;
            return this;
        }

        public Builder setAppInterface(AppInterface appInterface){
            this.appInterface = appInterface;
            return this;
        }

        public Builder setDataBaseName(String dbName) {
            dataBaseName = dbName;
            return this;
        }

        public Builder setDataBasePassword(String dbPassword) {
            dataBasePassword = dbPassword;
            return this;
        }

        public Builder setDataBaseVersion(int dbVersion) {
            dataBaseVersion = dbVersion;
            return this;
        }



        public final  void apply(){
            install(this);
        }

    }

}
